<?php

namespace app\admin\controller;

use app\api\model\CardsModel;
use app\common\controller\Backend;
use fast\Random;

/**
 * 学习卡管理

 *
 * @icon fa fa-circle-o
 */
class Card extends Backend
{

    /**
     * Card模型对象
     * @var \app\admin\model\Card
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Card;
        $this->view->assign("statusList", $this->model->getStatusList());
    }


    public function indexs (){
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $card_num = $params['card_num'];

            for($i=0 ;$i<$card_num;$i++){
                $data['day'] = $params['day'];
                $data['card_num'] =date("YmdHi").Random::numeric(6);
                $data['card_salt'] =Random::alnum(6);
                $data['card_password_yuan'] = Random::alnum(6);
                $data['card_password'] = md5(md5($data['card_password_yuan']).$data['card_salt']);
                $cardModel = new CardsModel();
                $res = $cardModel->save($data);
            }
            $this->success();
        }
        return $this->view->fetch();
    }
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if($row['status'] == 1){
            $this->error(__('该学习卡已失效'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $params['card_password'] = md5(md5($params['card_password_yuna']).$row['card_salt']);
            $result = $row->save($params);
            if ($result !== false) {
                $this->success();
            } else {
                $this->error(__('No rows were updated'));
            }
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
}
